﻿$(function () {
    var mytime = CurentTime();//获取当前时间
    $('#DateTime').datebox('setValue', mytime);
    getData();
    $(".t1 tr").mouseover(function () {
        $(this).addClass("over");
    }).mouseout(function () {
        $(this).removeClass("over");
    })
    $(".t1 tr:even").addClass("alt");

    initCtrl();
});

var _data = {
    theme: [
        { "title": "馒头", "name": "馒头" },
        { "title": "油饼", "name": "油饼" },
        { "title": "手抓饼", "name": "手抓饼" },
        { "title": "包子", "name": "包子" },
        { "title": "水饺", "name": "水饺" },
        { "title": "米饭", "name": "米饭" },
        { "title": "肉夹馍", "name": "肉夹馍" },
        { "title": "蛋炒饭", "name": "蛋炒饭" }
    ]
};
function initCtrl(type) {
    if (type == "" || type == "undefined" || type == undefined) { type = "馒头"; }
    $('#txt_Food').combobox({
        data: _data.theme, panelHeight: 'auto', editable: false, valueField: 'name', textField: 'title'
    });
    $('#txt_Food').combobox('setValue', type);

    $('#txt_NameI').textbox({
        onChange: function (value) {
            onNameIChange();
        }
    });
    $('#txt_NameII').textbox({
        onChange: function (value) {
            onNameIIChange();
        }
    });
    $('#txt_NameII').textbox({
        onChange: function (value) {
            onNameIIChange();
        }
    });
    $('#txt_FoodSum').textbox({
        onChange: function (value) {
            onFoodSumChange();
        }
    });
};

function insertData() {
    if ($("#txt_People").val() == "") {
        alert("订餐人不能为空！")
        return false;
    }
    $.ajax({
        url: '/Sky/ashx/Sky.ashx?action=InsertData',
        contentType: "application/json",
        dataType: "json",
        data: createData(),
        success: function (d) {
            getData();
        }
    })
}
function deleteData(id) {
    if (confirm("真的要删除吗？")) {
        $.ajax({
            url: '/Sky/ashx/Sky.ashx?action=DeleteData&Id=' + id,
            async: false,
            success: function (d) {
                if (d == "1") {
                    alert("删除成功！")
                } else {
                    alert("删除失败！")
                }
                getData();
            }
        })
        return true;
    } else {
        return false;
    }
}

function getData() {
    var time = $("#DateTime").datebox('getText');
    var html = "";
    var FoodMainCount = 0, Sum = 0;
    var food1 = "馒头"; var food1Sum = 0;
    var food2 = "蛋炒饭"; var food2Sum = 0;
    var food3 = "油饼"; var food3Sum = 0;
    var food4 = "包子"; var food4Sum = 0;
    var food5 = "水饺"; var food5Sum = 0;
    var food6 = "米饭"; var food6Sum = 0;
    var food7 = "肉夹馍"; var food7Sum = 0;
    var food8 = "手抓饼"; var food8Sum = 0;
    $.ajax({
        url: '/Sky/ashx/Sky.ashx?action=GetData' + '&DateTime=' + time,
        contentType: "application/json",
        dataType: "json",
        success: function (response, status, xhr) {
            $.each(response.Table, function (i, item) {
                if (item.FoodMain == food1) {
                    food1Sum += item.FoodMainCount;
                }
                if (item.FoodMain == food2) {
                    food2Sum += item.FoodMainCount;
                }
                if (item.FoodMain == food3) {
                    food3Sum += item.FoodMainCount;
                }
                if (item.FoodMain == food4) {
                    food4Sum += item.FoodMainCount;
                }
                if (item.FoodMain == food5) {
                    food5Sum += item.FoodMainCount;
                }
                if (item.FoodMain == food6) {
                    food6Sum += item.FoodMainCount;
                }
                if (item.FoodMain == food7) {
                    food7Sum += item.FoodMainCount;
                }
                if (item.FoodMain == food8) {
                    food8Sum += item.FoodMainCount;
                }
                Sum += item.Sum;
                html += '<tr>'
               + '<td>' + (i + 1).toString() + '</td>'
               + '<td>' + item.VegetableI + '</td>'
               + '<td style="color:red">' + item.VegetableIMoney + '</td>'
               + '<td>' + item.VegetableII + '</td>'
               + '<td style="color:red">' + item.VegetableIIMoney + '</td>'
               + '<td>' + item.FoodMain + '</td>'
               + '<td>' + item.FoodMainCount + '</td>'
               + '<td style="color:red">' + item.FoodMainMoney + '</td>'
               + '<td style="color:red">' + item.Sum + '</td>'
               + '<td>' + item.People + '</td>'
                if (item.Pay == "是") {
                    html += '<td></td>';
                } else {
                    html += '<td><a href="javascript:void(0)" onclick="deleteData(' + item.Id + ')">删除</a></td>';
                }
                + '</tr>';
            });
            html += '<tr>'
            html += '<td colspan="5" style="color: red;font-size:x-large">合计</td>'
            html += '<td colspan="2" >';

            if (food1Sum > 0) {
                html += food1 + ':' + food1Sum + '&nbsp;&nbsp;';
            }
            if (food2Sum > 0) {
                html += food2 + ':' + food2Sum + '&nbsp;&nbsp;';
            }

            if (food3Sum > 0) {
                html += food3 + ':' + food3Sum + '&nbsp;&nbsp;';
            }

            if (food4Sum > 0) {
                html += food4 + ':' + food4Sum + '&nbsp;&nbsp;';
            }

            if (food5Sum > 0) {
                html += food5 + ':' + food5Sum + '&nbsp;&nbsp;';
            }
            if (food6Sum > 0) {
                html += food6 + ':' + food6Sum + '&nbsp;&nbsp;';
            }
            if (food7Sum > 0) {
                html += food7 + ':' + food7Sum + '&nbsp;&nbsp;';
            }
            if (food8Sum > 0) {
                html += food8 + ':' + food8Sum + '&nbsp;&nbsp;';
            }
            html += '</td>';           
            html += '<td></td>'
            html += '<td style="width:40px; color: red;font-size:x-large">' + Sum + '</td>'
            html += '<td colspan="3"></td>'
            html += '</tr>';
            $('#OutTable tbody').html(html);
            getMenuData();
            GetStatus();
        }
    })
}
function getMenuData() {
    var time = $("#DateTime").datebox('getText');
    var html = "";
    var FoodMainCount = 0, Sum = 0;
    $.ajax({
        url: '/Sky/ashx/Sky.ashx?action=GetMenuData' + '&DateTime=' + time,
        contentType: "application/json",
        dataType: "json",
        success: function (response, status, xhr) {
            $.each(response.Table, function (i, item) {
                html = item.MenuContent;
            });
            $('#menuDiv').html(html);
        }
    })
}
function GetStatus() {
    var time = $("#DateTime").datebox('getText');
    var html = "";
    var FoodMainCount = 0, Sum = 0;
    $.ajax({
        url: '/Sky/ashx/Sky.ashx?action=GetStatus' + '&DateTime=' + time,
        contentType: "application/json",
        dataType: "json",
        success: function (d) {
            if (d == "1") {
                $("#MenuSave").hide();
            } else {
                $("#MenuSave").show();
            }
        }
    })
}
function createData() {
    var data = {
        "Id": $("#txt_Id").val(),
        "NameI": $("#txt_NameI").val(), "NameIM": $("#txt_NameIM").val(),
        "NameII": $("#txt_NameII").val(), "NameIIM": $("#txt_NameIIM").val(),
        "Food": $('#txt_Food').combobox('getValue'), "FoodSum": $("#txt_FoodSum").val(), "FoodM": $("#txt_FoodM").val(),
        "People": $("#txt_People").val(),
        "Pay": $("#txt_Pay").val(),
        "DateTime": $("#DateTime").datebox('getText')
    };
    return data;
}

function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
function myparser(s) {
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    } else {
        return new Date();
    }
}
function CurentTime() {
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
    var clock = year + "-";
    if (month < 10)
        clock += "0";
    clock += month + "-";
    if (day < 10)
        clock += "0";
    clock += day + " ";
    return (clock);
}

//芹菜猪肉饺子14肉三鲜饺子14精肉混沌12红烧鸡丁面12（大）10（小）西红柿鸡蛋面10咖喱土豆炖鸡肉7奥尔良炸鸡叉7辣炒鸡心6肉片有机菜花5炸萝卜肉丸5肉丝蒜黄5醋溜藕片5干锅娃娃菜5烧豆腐块5老厨黄豆芽5肉丝菠菜5西芹花生米5米饭1.5馒头0.5菠菜疙瘩汤
function getPrice(dish, menu) {
    var d = dish.replace(/[0-9.]+/g, "");
    var pos = menu.indexOf(d);
    var d_len = d.length;
    var m_s = menu.substring(pos + d_len);
    var price = parseFloat(m_s);
    return price;
}

function onNameIChange() {
    var d = $("#txt_NameI").textbox('getText');
    var p = $("#txt_NameIM").textbox('getText');
    d = d.replace(/\s+/g, "");  //去空格
    p = p.replace(/\s+/g, "");  //去空格

    if (d != "") {
        var menu = $('#menuDiv').html();
        var price = getPrice(d, menu);
        if (price != NaN && price > 0)
            $("#txt_NameIM").textbox('setValue', price);
    }
}

function onNameIIChange() {
    var d = $("#txt_NameII").textbox('getText');
    var p = $("#txt_NameIIM").textbox('getText');
    d = d.replace(/\s+/g, "");  //去空格
    p = p.replace(/\s+/g, "");  //去空格

    if (d != "") {
        var menu = $('#menuDiv').html();
        var price = getPrice(d, menu);
        if (price != NaN && price > 0)
            $("#txt_NameIIM").textbox('setValue', price);
    }
}

function onFoodSumChange() {
    var d = $("#txt_Food").textbox('getText');
    var c = $("#txt_FoodSum").textbox('getText');
    var p = $("#txt_FoodM").textbox('getText');
    d = d.replace(/\s+/g, "");  //去空格
    c = c.replace(/\s+/g, "");  //去空格
    p = p.replace(/\s+/g, "");  //去空格

    if (d != "" && c != "") {
        var menu = $('#menuDiv').html();
        var price = getPrice(d, menu);
        if (price != NaN && price > 0) {
            $("#txt_FoodM").textbox('setValue', price * c);
        }
    }
}